package com.smartum.mpr.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.smartum.mpr.exception.DatabaseIntegrityException;
import com.smartum.mpr.exception.NotCorrectPermission;
import com.smartum.mpr.exception.NotExistsClassyException;
import com.smartum.mpr.model.Classy;
import com.smartum.mpr.model.UploadedImageClassy;

@Repository
public class UploadedImageClassyDao extends GenericDao<Long, UploadedImageClassy> {


	public Classy getClassy(Long userId, Long imageId) throws NotExistsClassyException, DatabaseIntegrityException{
		DetachedCriteria criteria = DetachedCriteria.forClass(UploadedImageClassy.class);
		criteria.createCriteria("user").add(Restrictions.eq("id", userId));
		criteria.createCriteria("image").add(Restrictions.eq("id", imageId));

		List classy = hibernateTemplate.findByCriteria(criteria);

		if (classy.size() == 0) {
			throw new NotExistsClassyException();
		} else if (classy.size() > 1) {
			throw new DatabaseIntegrityException("database has few equals classys");
		} else if (classy.size() == 1) {
			return (Classy) classy.get(0);
		} else {
			throw new DatabaseIntegrityException();
		}
	}

	@Override
	protected Class<UploadedImageClassy> getEntityClass() {
		return UploadedImageClassy.class;
	}
}
